package org.yray.sip.creator;


import org.yray.sip.base.MsgEle;
import org.yray.sip.server.SipSession;
import org.yray.sip.utils.IdUtils;

public class InviteCreator extends MessageCreator {
    /***
     * Via: SIP/2.0/UDP 172.130.13.31:56691;rport;branch=z9hG4bKPjffaac0e69f1643988bbde864ec0eee79
     Max-Forwards: 70
     From: "78004" <sip:78004@172.168.200.78>;tag=8e72dc932e84437c9c225f744c485896
     To: <sip:77001@172.168.200.78>
     Contact: "78004" <sip:78004@172.130.13.31:56691;ob>
     Call-ID: 6655bda0bb4d4819a4e83229d74f003a
     CSeq: 3227 INVITE
     Route: <sip:172.168.200.78:5060;lr>
     Allow: PRACK, INVITE, ACK, BYE, CANCEL, UPDATE, INFO, SUBSCRIBE, NOTIFY, REFER, MESSAGE, OPTIONS
     Supported: replaces, 100rel, timer, norefersub
     Session-Expires: 1800
     Min-SE: 90
     User-Agent: MicroSIP/3.20.3
     Content-Type: application/sdp
     Content-Length:   318

     v=0
     o=- 3841406820 3841406820 IN IP4 172.130.13.31
     s=pjmedia
     b=AS:84
     t=0 0
     a=X-nat:0
     m=audio 4006 RTP/AVP 8 101
     c=IN IP4 172.130.13.31
     b=TIAS:64000
     a=rtcp:4007 IN IP4 172.130.13.31
     a=sendrecv
     a=rtpmap:8 PCMA/8000
     a=rtpmap:101 telephone-event/8000
     a=fmtp:101 0-16
     a=ssrc:121728904 cname:05972f40786a57ed

     * @param session
     * @return
     */
    @Override
    public String createMsg(SipSession session){
        StringBuilder sb = new StringBuilder();
        sb.append("INVITE sip:"+session.getTo()+"@"+session.getSipIpPort()+" SIP/2.0").append(MsgEle.LINE_SEPARATOR);
        sb.append("Via: SIP/2.0/UDP ").append(session.getLocalIpPort()).append(";rport;branch=").append(session.getBranch()).append(MsgEle.LINE_SEPARATOR);
        sb.append("Max-Forwards: 70").append(MsgEle.LINE_SEPARATOR);
        sb.append("From: "+session.getSipExtIpName()+";tag="+session.getTag()).append(MsgEle.LINE_SEPARATOR);
        sb.append("To: "+session.getToExt()).append(MsgEle.LINE_SEPARATOR);
        sb.append("Contact: "+session.getContact()).append(MsgEle.LINE_SEPARATOR);
        sb.append("Call-ID: "+session.getCallId()).append(MsgEle.LINE_SEPARATOR);
        sb.append("CSeq: "+session.getCseq()+" INVITE").append(MsgEle.LINE_SEPARATOR);
        sb.append("Route: <"+session.getSipIpPort()+";lr>").append(MsgEle.LINE_SEPARATOR);
        sb.append("Allow: PRACK, INVITE, ACK, BYE, CANCEL, UPDATE, INFO, SUBSCRIBE, NOTIFY, REFER, MESSAGE, OPTIONS").append(MsgEle.LINE_SEPARATOR);
        sb.append("Supported: replaces, 100rel, timer, norefersub").append(MsgEle.LINE_SEPARATOR);
        sb.append("Session-Expires: 1800").append(MsgEle.LINE_SEPARATOR);
        sb.append("Min-SE: 90").append(MsgEle.LINE_SEPARATOR);
        sb.append("User-Agent: ROBOT NO1").append(MsgEle.LINE_SEPARATOR);
        sb.append("Content-Type: application/sdp").append(MsgEle.LINE_SEPARATOR);

        int rtcpPort=session.getLocalPort()+11;
        int rtpPort=session.getLocalPort()+10;
        StringBuilder sdp=new StringBuilder();
        sdp.append("v=0").append(MsgEle.LINE_SEPARATOR);
        sdp.append("o=ROBOT "+ IdUtils.getId()+" 0 IN IP4 "+session.getLocalIp()).append(MsgEle.LINE_SEPARATOR);
        sdp.append("s=ROBOT").append(MsgEle.LINE_SEPARATOR);
        sdp.append("b=AS:84").append(MsgEle.LINE_SEPARATOR);
        sdp.append("t=0 0").append(MsgEle.LINE_SEPARATOR);
        sdp.append("a=X-nat:0").append(MsgEle.LINE_SEPARATOR);
        sdp.append("m=audio "+rtpPort+" RTP/AVP 8 101").append(MsgEle.LINE_SEPARATOR);
        sdp.append("c=IN IP4 "+session.getLocalIp()).append(MsgEle.LINE_SEPARATOR);
        sdp.append("b=TIAS:64000").append(MsgEle.LINE_SEPARATOR);
        sdp.append("a=rtcp:"+rtcpPort+" IN IP4 "+session.getLocalIp()).append(MsgEle.LINE_SEPARATOR);
        sdp.append("a=sendrecv").append(MsgEle.LINE_SEPARATOR);
        sdp.append("a=rtpmap:8 PCMA/8000").append(MsgEle.LINE_SEPARATOR);
        sdp.append("a=rtpmap:101 telephone-event/8000").append(MsgEle.LINE_SEPARATOR);
        sdp.append("a=fmtp:101 0-16").append(MsgEle.LINE_SEPARATOR);
        sdp.append("a=ssrc:"+session.getSsrc()+" cname:"+session.getCname()).append(MsgEle.LINE_SEPARATOR);
        sb.append("Content-Length:   ").append(sdp.length()).append(MsgEle.LINE_SEPARATOR);
        sb.append(MsgEle.LINE_SEPARATOR);
        sb.append(sdp.toString());
        sb.append(MsgEle.LINE_SEPARATOR);
        return sb.toString();
    }


}
